package com.xyf.sys.config;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiService;

import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payrollcard.PayrollCardService;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import com.wechat.pay.java.service.refund.RefundService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author
 * 微信服务商支付配置
 * @Date
 * @Description
 */
@Component
@ConfigurationProperties("wx")
@Setter
@Getter
public class WxConfig {

    /**
     * 商户号
     */
    public String merchantId;

    /**
     * 服务商户号 说明：服务商户号，由微信支付生成并下发
     */
    public String spMchid;
    /**
     * 子商户号 说明：子商户的商户号，由微信支付生成并下发
     */
    public String subMchid;
    /**
     * 商户API私钥路径
     */
    public String privateKey;
    /**
     * 商户证书序列号
     */
    public String merchantSerialNumber;
    /**
     * 商户APIV3密钥
     */
    public String apiV3Key;

    public String notifyUrl;

    public String refundNotifyUrl;

    private LaundryMerchant laundryMerchant;

    private DeliveryMerchant deliveryMerchant;

    @Bean()
    public Config defaultConfig() {
        // 一个商户号只能初始化一个配置
        Config config = new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .privateKey(privateKey)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
        return config;
    }

    @Bean
    public Config laundryConfig() {
        Config config = new RSAAutoCertificateConfig.Builder()
                .merchantId(laundryMerchant.getMchid())
                .privateKey(laundryMerchant.getPrivateKey())
                .merchantSerialNumber(laundryMerchant.getMerchantSerialNumber())
                .apiV3Key(laundryMerchant.getApiV3Key())
                .build();
        return config;
    }

    @Bean
    public Config deliveryConfig() {
        Config config = new RSAAutoCertificateConfig.Builder()
                .merchantId(deliveryMerchant.getMchid())
                .privateKey(deliveryMerchant.getPrivateKey())
                .merchantSerialNumber(deliveryMerchant.getMerchantSerialNumber())
                .apiV3Key(deliveryMerchant.getApiV3Key())
                .build();
        return config;
    }

    @Bean
    public JsapiService jsapiService(Config defaultConfig) {
        return new JsapiService.Builder().config(defaultConfig).build();
    }

    @Bean
    public com.wechat.pay.java.service.payments.jsapi.JsapiService laundryJsapiService(Config laundryConfig){
        return new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(laundryConfig).build();
    }

    @Bean
    public com.wechat.pay.java.service.payments.jsapi.JsapiService deliveryJsapiService(Config deliveryConfig){
        return new com.wechat.pay.java.service.payments.jsapi.JsapiService.Builder().config(deliveryConfig).build();
    }

    @Bean
    public RefundService refundService(Config defaultConfig) {
        return new RefundService.Builder().config(defaultConfig).build();
    }

    @Bean
    public RefundService laundryRefundService(Config laundryConfig) {
        return new RefundService.Builder().config(laundryConfig).build();
    }

    @Bean
    public RefundService deliveryRefundService(Config deliveryConfig) {
        return new RefundService.Builder().config(deliveryConfig).build();
    }

    @Bean
    public ProfitsharingService profitsharingService(Config defaultConfig) {
        return new ProfitsharingService.Builder().config(defaultConfig).build();
    }

    @Bean
    public PayrollCardService payrollCardService(Config defaultConfig) {
        return new PayrollCardService.Builder().config(defaultConfig).build();
    }

    @Getter
    @Setter
    public static class LaundryMerchant {
        private String mchid;
        private String privateKey;
        private String merchantSerialNumber;
        private String apiV3Key;
    }

    @Getter
    @Setter
    public static class DeliveryMerchant {
        private String mchid;
        private String privateKey;
        private String merchantSerialNumber;
        private String apiV3Key;
    }

}
